<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="110px"
      style="width: 80%; margin-left:50px;">
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="数据抽取名称" prop="extractionName">
            <el-input v-model="temp.extractionName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否启用" prop="status">
            <el-radio v-for="item in dictList('is_use')" :key="'type' + item.label" v-model="temp.status"
              :label="item.value">
              {{ item.label }}
            </el-radio>
          </el-form-item>
        </el-col>


      </el-row>
      <div style="margin-bottom: 20px;">
        <json-editor ref="jsonEditor" v-model="configJson" />
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">
        {{ $t('table.cancel') }}
      </el-button>
      <el-button type="primary" :loading="loading" @click="updateData">
        {{ $t('table.confirm') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createDataExtraction, updateDataExtraction, getDataExtraction } from '@/api/dataextraction/dataExtraction'
import JsonEditor from '@/components/JsonEditor'
export default {
  name: 'DataExtractionForm',
  components: { JsonEditor },
  data() {
    return {
      rules: {
        extractionName: [{ required: true, message: '数据抽取名称为必填项', trigger: 'blur' }],
        datasourceId: [{ required: true, message: '数据源id为必填项', trigger: 'blur' }],
        status: [{ required: true, message: '是否启用为必填项', trigger: 'blur' }],
        extractionType: [{ required: true, message: '数据抽取类型为必填项', trigger: 'blur' }]
      },
      temp: {},
      textMap: {
        update: '编辑',
        create: '新建'
      },
      loading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      configJson: ''
    }
  },
  methods: {
    getList() {
      this.$emit('refreshList')
    },
    resetTemp() {
      this.loading = false
      this.temp = {
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          createDataExtraction(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.getList()
              this.dialogFormVisible = false
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
    handleUpdate(row) {
      this.resetTemp()
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      // if(row.extractionType==0){}
      getDataExtraction(row.id).then(response => {
        if (response.data.code == 200) {
          this.temp = response.data.data
          let str = response.data.data.detailList[0].jobJson
          str=str.replaceAll('&amp;','&&')
          this.configJson=str
          if(typeof(str)=='string'){
            this.configJson =  JSON.parse(str)
            if(typeof(this.configJson)=='string'){
              this.configJson =  JSON.parse(this.configJson)
            }
          }

          // console.log(this.configJson)
        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }
      })
    },
    updateData() {
      console.log(this.configJson)
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          if(typeof(this.configJson)=='string'){
            this.temp.detailList[0].jobJson=this.configJson
            // this.configJson =  JSON.parse(str)
            // if(typeof(this.configJson)=='string'){
            //   this.configJson =  JSON.parse(this.configJson)
            // }
          }else{
            this.temp.detailList[0].jobJson=JSON.stringify(this.configJson)
            if(typeof(this.temp.detailList[0].jobJson)!='string'){
              let str2=this.temp.detailList[0].jobJson
              this.temp.detailList[0].jobJson=JSON.stringify(str2)
            }
          }

          updateDataExtraction(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.dialogFormVisible = false
              this.getList()
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
